package com.smart.common.security;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * http的通用拦截器，
 *
 * @author arnold.zhao
 * @date 2018/10/12 17:42
 */
public class LoginUrlAuthenticationEntryPoint extends org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        String requestURI = request.getRequestURI();
        String queryString = request.getQueryString();

        if (StringUtils.isNotBlank(queryString)) {
            requestURI = requestURI + "?" + queryString;
        }

        //存储当前拦截器的访问路径，登录成功后，直接跳转到对应的路径中
        request.getSession().setAttribute("requestURI", requestURI);

        super.commence(request, response, authException);
    }
}
